package com.ruoyi.sl.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.sl.domain.SlDeduction;
import com.ruoyi.sl.mapper.SlDmMapper;
import com.ruoyi.sl.domain.SlDm;
import com.ruoyi.sl.service.ISlDmService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 报工项扣款Service业务层处理
 * 
 * @author admin
 * @date 2024-07-05
 */
@Service
public class SlDmServiceImpl implements ISlDmService 
{
    @Autowired
    private SlDmMapper slDmMapper;

    /**
     * 查询报工项扣款
     * 
     * @param dmId 报工项扣款主键
     * @return 报工项扣款
     */
    @Override
    public SlDm selectSlDmByDmId(Long dmId)
    {
        return slDmMapper.selectSlDmByDmId(dmId);
    }

    /**
     * 查询报工项扣款列表
     * 
     * @param slDm 报工项扣款
     * @return 报工项扣款
     */
    @Override
    public List<SlDm> selectSlDmList(SlDm slDm)
    {
        return slDmMapper.selectSlDmList(slDm);
    }

    /**
     * 新增报工项扣款
     * 
     * @param slDm 报工项扣款
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSlDm(SlDm slDm)
    {
        slDm.setCreateTime(DateUtils.getNowDate());
        slDm.setCreateBy(getLoginUser().getUsername());
        slDm.setDmNick(getLoginUser().getUser().getNickName());
        int rows = slDmMapper.insertSlDm(slDm);
        insertSlDeduction(slDm);
        return rows;
    }

    /**
     * 修改报工项扣款
     * 
     * @param slDm 报工项扣款
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSlDm(SlDm slDm)
    {
        slDm.setUpdateTime(DateUtils.getNowDate());
        slDm.setUpdateBy(getLoginUser().getUsername());
        slDmMapper.deleteSlDeductionByDmId(slDm.getDmId());
        insertSlDeduction(slDm);
        return slDmMapper.updateSlDm(slDm);
    }

    //将数据状态更改为已结算
    @Override
    public int updateSlDmIsSettle(Long dmId){
        return slDmMapper.updateSlDmIsSettle(dmId);
    }

    /**
     * 批量删除报工项扣款
     * 
     * @param dmIds 需要删除的报工项扣款主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSlDmByDmIds(Long[] dmIds)
    {
        slDmMapper.deleteSlDeductionByDmIds(dmIds);
        return slDmMapper.deleteSlDmByDmIds(dmIds);
    }

    /**
     * 删除报工项扣款信息
     * 
     * @param dmId 报工项扣款主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSlDmByDmId(Long dmId)
    {
        slDmMapper.deleteSlDeductionByDmId(dmId);
        return slDmMapper.deleteSlDmByDmId(dmId);
    }

    /**
     * 新增扣款记录信息
     * 
     * @param slDm 报工项扣款对象
     */
    public void insertSlDeduction(SlDm slDm)
    {
        List<SlDeduction> slDeductionList = slDm.getSlDeductionList();
        Long dmId = slDm.getDmId();
        if (StringUtils.isNotNull(slDeductionList))
        {
            List<SlDeduction> list = new ArrayList<SlDeduction>();
            for (SlDeduction slDeduction : slDeductionList)
            {
                slDeduction.setDmId(dmId);
                slDeduction.setRfworkId(slDm.getRfworkId());
                slDeduction.setRworkId(slDm.getRworkId());
                slDeduction.setDmAmount(slDm.getDmAmount());
                slDeduction.setDmReason(slDm.getDmReason());
                slDeduction.setDmTime(slDm.getDmTime());
                slDeduction.setIsSettle(slDm.getIsSettle());
                list.add(slDeduction);
            }
            if (list.size() > 0)
            {
                slDmMapper.batchSlDeduction(list);
            }
        }
    }
}
